package com.sky.controller.user;

import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端购物车接口操作")
public class ShoppingCartController {

    @Autowired
    ShoppingCartService service;

    /**
     * 添加购物车接口
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    public Result addShippingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("购物车添加：{}",shoppingCartDTO);
        service.addCart(shoppingCartDTO);
        return Result.success();
    }


    /**
     * 获取用户购物车数据
     * @return List<ShoppingCart>
     */
    @GetMapping("/list")
    public Result<List<ShoppingCart>> getShoppingCart(){
        List<ShoppingCart> shoppingCartList =  service.getCart();
        return Result.success(shoppingCartList);
    }

    /**
     * 删除所有的购物车数据
     * @return
     */
    @DeleteMapping("/clean")
    public Result deleteAllShoppingCart(){
        service.deleteAll();
        return Result.success();
    }

    //删除一个菜品
    @PostMapping("/sub")
    public Result deleteShoppingCart(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("删除一条购物车数据{}",shoppingCartDTO);
        service.delete(shoppingCartDTO);
        return Result.success();
    }
}
